﻿using DevExpress.Data.Filtering;
using System.Linq.Expressions;
using Volo.Abp.DependencyInjection;
using YAPACS.BusinessObjects.Exams;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Match;

public class StudyConvertExpression : WorkflowBase, IStudyConvertExpression, ITransientDependency
{
    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Input";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Dictionary<string, object> search)
        {
            Expression<Func<BusinessObjects.Exams.Study, bool>> expression = (e) => search.Count != 0;
            if (search.ContainsKey("accessionNumber") || search.ContainsKey("patientId") || search.ContainsKey("name"))
            {
                input.Add("Precision", true);
            }
            //查询字段单个分析
            //检查号
            if (search.ContainsKey("accessionNumber"))
            {
                if (search.GetValueOrDefault("accessionNumber") is string value)
                {
                    expression = expression.And(e => e.AccessionNumber == value);
                }
            }
            //患者Id
            if (search.ContainsKey("patientId"))
            {
                if (search.GetValueOrDefault("patientId") is string value)
                {
                    expression = expression.And(e => e.PatientId == value);
                }
            }
            //姓名
            if (search.ContainsKey("name"))
            {
                if (search.GetValueOrDefault("name") is string value)
                {
                    expression = expression.And(e => e.PatientName == value);
                }
            }
            //性别
            if (search.ContainsKey("sex"))
            {
                if (search.GetValueOrDefault("sex") is string value)
                {
                    expression = expression.And(e => e.PatientSex == value);
                }
            }
            //设备
            if (search.ContainsKey("deviceTypes"))
            {
                if (search.GetValueOrDefault("deviceTypes") is string value)
                {
                    if (!(string.IsNullOrWhiteSpace(value) || value == "全部"))
                    {
                        var deviceTypes = value.Split(',');
                        var criteria = CriteriaOperator.FromLambda<Exam>(e => e.DeviceType != null && deviceTypes.Contains(e.DeviceType.Code));
                        expression = expression.And(e => e.Device != null && e.Device.DeviceType != null && deviceTypes.Contains(e.Device.DeviceType.Code));
                    }
                }
            }
            //检查日期
            if (!search.ContainsKey("examTimeRange"))
            {
                search.Add("examTimeRange", string.Join(",", [DateTime.Today.ToString("yyyy-MM-dd"), DateTime.Today.ToString("yyyy-MM-dd")]));
            }
            if (search.ContainsKey("examTimeRange"))
            {
                if (search.GetValueOrDefault("examTimeRange") is string value)
                {
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        value = string.Join(",", [DateTime.Today.ToString("yyyy-MM-dd"), DateTime.Today.ToString("yyyy-MM-dd")]);
                    }
                    var timeRange = value.Split(',');
                    var startTime = DateTime.Parse(timeRange[0]).Date;
                    var endTime = DateTime.Parse(timeRange[1]).AddDays(1).Date;

                    expression = expression.And(e => (e.StudyDateTime >= startTime && e.StudyDateTime < endTime) || (e.CreationTime >= startTime && e.CreationTime < endTime));
                }
            }
            input.Add("Output", expression);
        }
        return await Task.FromResult(new WorkflowOutput
        {
            Success = true
        });
    }
}
